<template>
  <div>
    <div class="input-group">
      <input
        style="margin-bottom: 20px; width: 300px"
        v-model="props.email"
        type="email"
        disabled
        placeholder="Enter your email"
      />
    </div>
    <div class="input-group code-group">
      <div class="code-input">
        <input
          v-model="formData.code"
          type="text"
          placeholder="Enter verification code"
        />
        <button
          :class="['send-btn', { disabled: isCounting }]"
          :disabled="isCounting"
          @click="sendVerificationCode(props.email)"
        >
          {{ countdown > 0 ? `${countdown}s` : "Send Code" }}
        </button>
      </div>
      <div v-if="isReg" class="error-message">
        Email has been registered!
      </div>
    </div>
    <button class="submit-btn" @click="handleSubmit">Submit</button>
  </div>
</template>

<script lang="ts" setup>
import { postSendEmailCode,postVerifyEmail } from "@/api/index";
// import { useUserCounter } from "@/pinia/user";
import { message } from 'ant-design-vue';
// const userStore = useUserCounter();
const userInfo = ref<any>(null);
import { ref, reactive, defineProps } from "vue";
import router from "../router";
const emit = defineEmits(["dialogVisible"]);
const props = defineProps({
  email: {
    type: String,
    required: "",
  },
});
const formData = reactive({
  email: "",
  code: "",
});
// 倒计时相关
const countdown = ref(0);
const isCounting = ref(false);
let timer = null;
const isReg = ref(false)
// 发送验证码
const sendVerificationCode = async (val) => {
  if (!val) {
    message.warning("Please enter email.")
    return;
  }
  // 开始倒计时
  isCounting.value = true;
  countdown.value = 60;
  timer = setInterval(() => {
    countdown.value--;
    if (countdown.value <= 0) {
      clearInterval(timer);
      isCounting.value = false;
    }
  }, 1000);
  await postSendEmailCode({ email: val }).then((res) => {
    if (res.code === 200) {
      message.success("Successfully sent")
      return;
    }else if (res.code === 40003) {
      message.error("Email has been registered!")
      isReg.value = true
      setTimeout(() => {
        isReg.value = false
      }, 4000);
      return;
    } else {
      message.error(`${res.msg}`)
      return;
    }
  });
};
// 处理提交
const handleSubmit = async () => {
    formData.email = props.email;
  if (!formData.code) {
    // alert("Please fill all fields");
    message.warning('Please fill all fields.')
    return;
  }

  // 这里添加实际提交逻辑
  // console.log("Submit:", formData);
  await postVerifyEmail(formData).then((res) => {
    if (res.code === 200) {
      message.success(`${res.msg}`)
      router.push('/user/points')
      //更新用户信息
      // userStore.setUserInfo();
      // 关闭对话框
    emit("dialogVisible", false);

    } else {
      const msg = res.msg
      message.error(msg)
    }
  });
};
</script>

<style scoped lang="scss">
.input-group label {
  display: block;
  font-size: 0.875rem;
  color: #333;
  margin-bottom: 8px;
}

.input-group input {
  //   width: 300px;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 0.875rem;
}

.code-group .code-input {
  display: flex;
  gap: 10px;
}

.code-group input {
  flex: 1;
}
.send-btn {
  background: #000;
  color: white;
  border: none;
  padding: 12px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.875rem;
  transition: all 0.3s;
}

.send-btn.disabled {
  background: #999;
  cursor: not-allowed;
}
.submit-btn {
  width: 100%;
  background: #000;
  color: white;
  padding: 12px 20px;
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  cursor: pointer;
  transition: opacity 0.3s;
  margin-top: 20px;
}

.submit-btn:hover {
  opacity: 0.9;
}
.error-message {
  color: red;
  margin-top: 10px;
  font-size: 0.75rem;
}
</style>
